<?php

namespace plugin\admin\app\controller\Crontab;

use plugin\admin\app\controller\Base;
use support\Request;
use yzh52521\Task\Client;

class Cron extends Base
{
    public function list(Request $request){
        $get = $request->get();
        $page = $get['page'] ?? 1;
        $limit = $get['limit'] ?? 15;
        $param = [
            'method' => 'crontabIndex',//计划任务列表
            'args'   => ['limit' => $limit, 'page' => $page]//参数
        ];
        $result= Client::instance()->request($param);
        return json($result);
    }
    public function addOrEdit(Request $request)
    {
        if ($request->method() != 'POST') {
            abort(1001,'操作失败,请求类型错误');
        }
        $data  = $request->post();
        $type = $data['mode'];
        $form = $data['form'];
        $param = [
            'method' => 'crontabCreate',
            'args'   => [
                'title'     => $form['title'],
                'type' =>   $form['type'],
                'rule' =>   $form['rule'],
                'target'     => $form['target'],
                'parameter'     => $form['parameter'],
                'remark'     => $form['remark'],
                'status'     => $form['status'],
                'singleton'     => $form['singleton']
            ]
        ];
        if($type == 'edit'){
            $param['method'] = 'crontabUpdate';
            $param['args']['id'] = $form['id'];
        }
        $result  = Client::instance()->request($param);
        return json($result);
    }
    public function delete(Request $request)
    {
        if ($request->method() != 'POST') {
            abort(1001,'操作失败,请求类型错误');
        }
        $data  = $request->post();
        $param = [
            'method' => 'crontabDelete',
            'args'   => [
                'id'     =>strval($data['id'])
            ]
        ];
        $result  = Client::instance()->request($param);
        return json($result);
    }
    public function reload(Request $request)
    {
        if ($request->method() != 'POST') {
            abort(1001,'操作失败,请求类型错误');
        }
        $data  = $request->post();
        $param = [
            'method' => 'crontabReload',
            'args'   => [
                'id'     =>strval($data['id'])
            ]
        ];
        $result  = Client::instance()->request($param);
        return json($result);
    }
    public function logList(Request $request){
         $get = $request->get();
         $page = $get['page'] ?? 1;
         $limit = $get['limit'] ?? 15;
         $crontab_id = $get['id'] ?? 1;

        $param = [
            'method' => 'crontabLog',
            'args'   => [
                'page'     => $page,
                'limit' => $limit,
                'crontab_id' => $crontab_id
            ]
        ];
        $result  = Client::instance()->request($param);
        return json($result);
    }
}
